/****************************************Copyright (c)**************************************************
**                                         2016-03
**
**                                
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name:		    ht6x1x_gpio.c
** Last modified Date:  2016-03-25
** Last Version:		v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Created by:			Evan Chen
** Created date:		2016-03-25
** Version:				v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "ht6x1x_gpio.h"
#include "ht6x1x_flash.h"

void GPIO_Init(HT_GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
  uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  
  WPREG_Disable();
  /*-------------------------- Configure the port pins -----------------------*/
  /*-- GPIO Mode Configuration --*/
  for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  {
    pos = ((uint32_t)0x01) << pinpos;

    /* Get the port pins position */
    currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;

    if (currentpin == pos)
    {
      if(GPIO_Mode_AF == GPIO_InitStruct->GPIO_Mode)
      {
		GPIOx->IOCFG |= currentpin;

		if(GPIO_AF_0 == GPIO_InitStruct->GPIO_PinAf)
		{
		  GPIOx->AFCFG &= (~currentpin);
		}
		else
		{
			GPIOx->AFCFG |= currentpin;
		}
      }
	  else 
	  {
        GPIOx->IOCFG &= (~currentpin);
		
		if(GPIO_Mode_IN == GPIO_InitStruct->GPIO_Mode)
		{
		  GPIOx->PTDIR &= (~currentpin);

		  if(GPIO_PuPd_UP == GPIO_InitStruct->GPIO_PuPd)
		  {
		    GPIOx->PTUP &= (~currentpin);
		  }
		  else
		  {
		    GPIOx->PTUP |= currentpin;
		  }		  
		}
		else
		{
		  GPIOx->PTDIR |= currentpin;

		  if(GPIO_OType_OD == GPIO_InitStruct->GPIO_OType)
		  {
		    GPIOx->PTOD &= (~currentpin);
		  }
		  else
		  {
		    GPIOx->PTOD |= currentpin;
		  }
		}
	  }
    }
  }

  WPREG_Enable();
}


void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
{
  /* Reset GPIO init structure parameters values */
  GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
  GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN; 
  GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStruct->GPIO_PinAf = GPIO_AF_0;
}

uint8_t GPIO_ReadInputDataBit(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  uint8_t bitstatus = 0x00;

  if ((GPIOx->PTDAT & GPIO_Pin) != (uint32_t)Bit_RESET)
  {
    bitstatus = (uint8_t)Bit_SET;
  }
  else
  {
    bitstatus = (uint8_t)Bit_RESET;
  }
  
  return bitstatus;
}

uint16_t GPIO_ReadInputData(HT_GPIO_TypeDef* GPIOx)
{
  return ((uint16_t)GPIOx->PTDAT);
}

uint8_t GPIO_ReadOutputDataBit(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  uint8_t bitstatus = 0x00;

  if ((GPIOx->PTDAT & GPIO_Pin) != (uint32_t)Bit_RESET)
  {
    bitstatus = (uint8_t)Bit_SET;
  }
  else
  {
    bitstatus = (uint8_t)Bit_RESET;
  }
  
  return bitstatus;
}

uint16_t GPIO_ReadOutputData(HT_GPIO_TypeDef* GPIOx)
{
  return ((uint16_t)GPIOx->PTDAT);
}
void GPIO_ClrBits(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  GPIOx->PTCLR = GPIO_Pin;
}
void GPIO_SetBits(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  GPIOx->PTSET = GPIO_Pin;
}

void GPIO_ResetBits(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  GPIOx->PTCLR = GPIO_Pin;
}

void GPIO_WriteBit(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
{
  if (BitVal != Bit_RESET)
  {
    GPIOx->PTSET = GPIO_Pin;
  }
  else
  {
    GPIOx->PTCLR = GPIO_Pin ;
  }
}

void GPIO_Write(HT_GPIO_TypeDef* GPIOx, uint16_t PortVal)
{
  GPIOx->PTDAT = PortVal;
}

void GPIO_Toggle(HT_GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	GPIOx->PTTOG = GPIO_Pin;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
